<template>
    <div class="app-container">
        <!-- 管家绑定医院 -->
        <h5 class="titleInfo">管家绑定医院</h5>
        <el-form :inline="true" label-width="130px">
            <el-transfer
                v-loading="loadings"
                filterable
                class="transfer-new-style"
                :titles="titles"
                :button-texts="['到左边', '到右边']"
                :filter-method="filterMethod"
                filter-placeholder="请输入医院"
                v-model="value"
                :data="hospitalData"
            ></el-transfer>
            <el-form-item>
                <el-button
                    style="margin-top:30px;margin-left:130px"
                    type="primary"
                    @click="submitFn()"
                >立即提交</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
import { bindButlerHospital, butlerDetail } from "@/api/stewardItem";
import { hospitalDropDown } from "@/api/doctorManage";
export default {
    name: "stewardItemEdit",
    data() {
        return {
            loadings:true,
            hospitalData: [],
            value: [],
            titles: ["医院列表", "已勾选医院"],
            filterMethod(query, item) {
                return item.py.indexOf(query) > -1;
            },
            userId: null,
            officeId: ""
        };
    },
    inject: ["reload"],
    mounted() {
        this.userId = Number(this.$route.query.userId);
        this.getDetail();
    },
    methods: {
        //获取医院列表
        getHospital(officeId) {
            hospitalDropDown().then(res => {
                // console.log(res)
                let resData = res.data.data;
                let newData = [];
                resData.forEach(e => {
                    if(officeId == "21"){
                        newData.push({
                            key: e.id + "",
                            label: e.name,
                            py: e.name
                        });
                    }else{
                        if (officeId == e.office || e.office == "21") {
                            newData.push({
                                key: e.id + "",
                                label: e.name,
                                py: e.name
                            });
                        }
                    }
                });
                this.hospitalData = newData;
                this.loadings = false
            });
        },
        //获取医院详情
        getDetail() {
            let datas = {
                id: this.userId
            };
            butlerDetail(datas)
                .then(res => {
                    if (res.data.code == 0) {
                        if (res.data.data.hospitalIdList) {
                            this.value = res.data.data.hospitalIdList.split(
                                ","
                            );
                        }
                        this.officeId = res.data.data.officeId + "";
                        this.getHospital(this.officeId);
                    } else {
                        this.$message.error(res.data.msg);
                    }
                })
                .catch(error => {
                    this.$message.error(error);
                });
        },
        //提交
        submitFn() {
            if (this.value.length > 0) {
                let datas = {
                    id: this.userId,
                    hospitalList: this.value.join(",")
                };
                bindButlerHospital(datas)
                    .then(res => {
                        // console.log(res)
                        if (!res.data.code) {
                            this.$message({
                                showClose: true,
                                message: "绑定成功",
                                type: "success"
                            });
                            this.$router.push("/stewardItemIndex");
                            this.reload();
                        } else {
                            this.$message.error(res.data.msg);
                        }
                    })
                    .catch(error => {
                        this.$message.error(error);
                    });
            } else {
                this.$message({
                    message: "未选择医院",
                    type: "warning"
                });
            }
        }
    }
};
</script>

<style scoped>
.app-container {
    padding: 0px !important;
}
.form {
    margin-left: 268px;
}
</style>